package com.starbaba.core.web.taglib.component;

import java.io.Writer;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;

import com.starbaba.core.web.translate.TranslateUtils;
import com.starbaba.core.web.translate.TranslateUtilsFactory;
import com.opensymphony.xwork2.util.ValueStack;


public class Code3NameComponent extends Component{
	
	static Logger logger = Logger.getLogger(TranslateUtils.class);
	
	private String definition;
	private String value;

	public Code3NameComponent(ValueStack stack) {
		super(stack);
	}
	
	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer); 
		try {
			String targetName = this.getOutputMessage();
			if(StringUtils.isNotBlank(targetName)){
				writer.write(targetName);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
        return result;
    }
	
	public String getOutputMessage(){
		String targetName = "";
		try{
			TranslateUtils tu = TranslateUtilsFactory.getInstance();
			if(null == tu) {
				targetName = this.getValue();
			}else{
				targetName = tu.getName_table(definition, value);
			}
		}catch(Exception ex){
			logger.error("TranslateUtils[definition=" + definition + "|value=" + value + "]", ex);
			logger.error(ex.getMessage());
			targetName = this.getValue();
		}
		return targetName;
	}

	public String getDefinition() {
		return definition;
	}

	public void setDefinition(String definition) {
		this.definition = definition;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

}
